Shader "Unlit/Shelter"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _ShelterColor("ShelterColor", Color) = (1, 1, 1, 1)
        //菲涅尔反射 垂直角度反射率
        _FresnelScale("FresnelScale",Range(0,2)) = 1
        //菲涅尔近似公式 n次方
        _FresnelN ("FresnelN",Range(0,5)) = 5
    }
    SubShader
    {
        Tags
        {
            "RenderType"="Opaque"
        }
        LOD 100

        Pass
        {
            //当前面有物体遮挡时，这个pass才会通过
            ZTest Greater
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog
            #include "UnityCG.cginc"

            fixed4 _ShelterColor;
            half _Alpha;
            float _FresnelScale;
            float _FresnelN;
            

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float3 worldViewDir : TEXCOORD0;//世界空间下视角方向
                float worldNormal : TEXCOORD1;//世界空间下法线方向
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;


            //菲涅尔近似公式
            
            float Fresnel(float3 normal, float3 viewDir)
            {
                float fresnel = pow(1 - saturate(dot(normal, viewDir)), _FresnelN);
                return fresnel * _FresnelScale;
            }

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.worldViewDir = UnityObjectToWorldDir(normalize(ObjSpaceViewDir(v.vertex)));
                o.worldNormal = normalize(UnityObjectToWorldNormal(v.vertex));
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                float v = pow(1 - dot(i.worldNormal, i.worldViewDir), _FresnelN);
                fixed alpha = _FresnelScale + (1 - _FresnelScale) * v;
                return fixed4(_ShelterColor.rgb, alpha);
            }
            ENDCG
        }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                return col;
            }
            ENDCG
        }
    }
}